//
//  FGSeatFromTemplateView.m
//  lite
//
//  Created by feige on 2022/11/4.
//

#import "FGSeatFromTemplateView.h"
#import "UIView+FGLayout.h"

@interface FGSeatFromTemplateView()<UITableViewDataSource>
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) id templateData;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) UIButton *submitButton;
@end

@implementation FGSeatFromTemplateView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        // titleLabel
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:_titleLabel];
        
        // tableView
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.rowHeight = 40;
        [self addSubview:_tableView];
        
        // submitButton
        _submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _submitButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_submitButton setTitle:@"提交" forState:0];
        [_submitButton setTitleColor:[UIColor whiteColor] forState:0];
        _submitButton.backgroundColor = [UIColor blueColor];
        _submitButton.layer.cornerRadius = 20;
        _submitButton.layer.masksToBounds = YES;
        [_submitButton addTarget:self action:@selector(submitAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_submitButton];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _titleLabel.mm_sizeToFit().mm_right(10).mm_left(10).mm_top(10);
    _tableView.frame = CGRectMake(0, 40, self.mm_w, self.mm_h - 100);
    _submitButton.frame = CGRectMake(10, self.mm_h - 50, self.mm_w - 20, 40);
}

#pragma mark - Rewrite Mehtod

- (void)fillData:(id)templateData {
    self.templateData = templateData;
    
    self.titleLabel.text = [self.templateData valueForKeyPath:@"data.prompt"];
    self.dataArray = [self.templateData valueForKeyPath:@"data.fileds"];
    [self.tableView reloadData];
}

#pragma mark - Response Event

- (void)submitAction:(UIButton *)sender
{
    NSArray *cells = self.tableView.visibleCells;
    
    NSMutableArray *arr = [NSMutableArray new];
    [cells enumerateObjectsUsingBlock:^(UITableViewCell *cell, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *value = ((UITextField *)[cell.contentView viewWithTag:1001]).text;
        NSDictionary *oDict = self.dataArray[idx];
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict addEntriesFromDictionary:oDict];
        dict[@"value"] = value;
        [arr addObject:dict];
    }];
    
    if ([self.delegate respondsToSelector:@selector(seatFromTemplateViewSubmitFrom:)]) {
        [self.delegate seatFromTemplateViewSubmitFrom:arr];
    }
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FGSeatFromTemplateViewCell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FGSeatFromTemplateViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 5, tableView.mm_w - 20, 30)];
        textField.font = [UIFont systemFontOfSize:14];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.tag = 1001;
        [cell.contentView addSubview:textField];
    }
    
    // {"formId":2, "id":152, "isRequired":true, "labelName":"CompanyName", "fieldName":"公司名称", "sort":0}
    NSDictionary *dict = self.dataArray[indexPath.row];
    
    UITextField *textField = [cell.contentView viewWithTag:1001];
    textField.placeholder = [NSString stringWithFormat:@"%@%@", dict[@"fieldName"], [dict[@"isRequired"] boolValue] ? @"（必填）" : @""];
    
    return cell;
}

@end
